package com.tudouji.project.wlinfo.controller;

import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.interceptor.annotation.RepeatSubmit;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.organization.domain.Supplier;
import com.tudouji.project.organization.mapper.SupplierMapper;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.service.impl.SalespermissionServiceImpl;
import com.tudouji.project.system.domain.SysUser;
import com.tudouji.project.system.domain.Tuser;
import com.tudouji.project.system.service.ITuserService;
import com.tudouji.project.wlinfo.domain.Wlinfo;
import com.tudouji.project.wlinfo.mapper.WlinfoMapper;
import com.tudouji.project.wlinfo.service.IWlinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 物流信息Controller
 *
 * @author tudouji
 * @date 2021-10-27
 */
@RestController
@RequestMapping("/wlinfo/wlinfo")
public class WlinfoController extends BaseController {
	@Autowired
	private IWlinfoService wlinfoService;
	@Autowired
	private ITuserService userService;
	@Autowired
	private SalespermissionServiceImpl permissionService;

	@Autowired
	private WlinfoMapper wlinfoMapper;

	@Autowired
	private SupplierMapper supplierMapper;

	/**
	 * 查询物流信息列表
	 */
	// @PreAuthorize("@ss.hasPermi('wlinfo:wlinfo:list')")
	@GetMapping("/list")
	public TableDataInfo list(Wlinfo wlinfo, String functiontype) {
		List<Wlinfo> list =new ArrayList<Wlinfo>();
		SysUser sysUser = SecurityUtils.getLoginUser().getUser();
		Tuser user = userService.selectTuserByName(sysUser.getPhonenumber());
		List<Salespermission> permissions = permissionService.getPermByScAndFunc(user.getGuid(), functiontype, "4");
		if (permissions.size() > 0) {
			startPage();
			list= wlinfoMapper.selectWlinfoListByPerm(wlinfo);
		} else {
			wlinfo.setOperatorid(user.getGuid());
			permissions = permissionService.getPermByScAndFunc(user.getGuid(), functiontype, "2");// 部门权限
			if (permissions.size() > 0) {
				String[] departids = new String[permissions.size()];
				for (int i = 0; i < permissions.size(); i++) {
					departids[i] = permissions.get(i).getDepartid();
				}
				wlinfo.setDepartids(departids);
			}
			startPage();
			list= wlinfoMapper.selectWlinfoListByPerm(wlinfo);
		}
		return getDataTable(list);
	}

	/**
	 * 导出物流信息列表
	 */
	@Log(title = "物流信息", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(Wlinfo wlinfo, String functiontype) {
		List<Wlinfo> list =new ArrayList<Wlinfo>();
		SysUser sysUser = SecurityUtils.getLoginUser().getUser();
		Tuser user = userService.selectTuserByName(sysUser.getPhonenumber());
		logger.info("-----------------dao chu-----------------");
		List<Salespermission> permissions = permissionService.getPermByScAndFunc(user.getGuid(), functiontype, "4");
		if (permissions.size() > 0) {
			startPage();
			list= wlinfoMapper.selectWlinfoListByPerm(wlinfo);
		} else {
			wlinfo.setOperatorid(user.getGuid());
			permissions = permissionService.getPermByScAndFunc(user.getGuid(), functiontype, "2");// 部门权限
			if (permissions.size() > 0) {
				String[] departids = new String[permissions.size()];
				for (int i = 0; i < permissions.size(); i++) {
					departids[i] = permissions.get(i).getDepartid();
				}
				wlinfo.setDepartids(departids);
			}
			startPage();
			list= wlinfoMapper.selectWlinfoListByPerm(wlinfo);
		}
		ExcelUtil<Wlinfo> util = new ExcelUtil<Wlinfo>(Wlinfo.class);
		return util.exportExcel(list, "wlinfo");
	}

	/**
	 * 获取物流信息详细信息
	 *
	 */
//	@PreAuthorize("@ss.hasPermi('wlinfo:wlinfo:query')")
	@GetMapping(value = "/{guid}")
	public AjaxResult getInfo(@PathVariable("guid") String guid) {
		return AjaxResult.success(wlinfoService.selectWlinfoById(guid));
	}

	/**
	 * 新增物流信息
	 */
	@PreAuthorize("@ss.hasPermi('wlinfo:wlinfo:add')")
	@Log(title = "物流信息", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Wlinfo wlinfo) {
		return toAjax(wlinfoService.insertWlinfo(wlinfo));
	}

	/**
	 * 修改物流信息
	 */
//	@PreAuthorize("@ss.hasPermi('wlinfo:wlinfo:edit')")
	@Log(title = "物流信息", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Wlinfo wlinfo) {
		return toAjax(wlinfoService.updateWlinfo(wlinfo));
	}

	/**
	 * 删除物流信息
	 */
	@PreAuthorize("@ss.hasPermi('wlinfo:wlinfo:remove')")
	@Log(title = "物流信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
	public AjaxResult remove(@PathVariable String[] guids) {
		return toAjax(wlinfoService.deleteWlinfoByIds(guids));
	}

	/**
	 * 获取物流待结算信息 :获取待支付状态status=30、尚未发起付款paystatus=10的物流单接口
	 */
	@GetMapping("/getWlInfoList")
	public TableDataInfo getWlInfoList(String orgid, String customerid, String startdate, String enddate,
			String functiontype, String status, String paystatus) {
		startPage();
		List<Wlinfo> list = wlinfoService.getWlInfoList(orgid, customerid, startdate, enddate, functiontype, status,
				paystatus);
		return getDataTable(list);
	}

	/**
	 * 发货
	 *
	 * @param guid
	 * @return
	 */
	@RepeatSubmit
	@GetMapping("/deliver")
	public AjaxResult deliver(String guid) {
		return toAjax(wlinfoService.deliver(guid));
	}

	/**
	 * 运费结算
	 *
	 * @author xxl
	 * @return
	 */
	@RepeatSubmit
	@PostMapping("/settle")
	public AjaxResult settle(@RequestBody Wlinfo wlinfo) {
		return wlinfoService.settle(wlinfo);
	}

	/**
	 * 反结算
	 */
	@RepeatSubmit
	@GetMapping("/backSettle")
	public AjaxResult backSettle(String guid) {
		return wlinfoService.backSettle(guid);
	}

	/**
	 * 物流单校验
	 */
	@GetMapping("/checkOrder")
	public AjaxResult checkOrder(String guid) {
		return wlinfoService.checkOrder(guid);
	}


	/**
	 * 导出物流对账单EXCEL
	 */
	@GetMapping("/exportWlInfoList")
	public AjaxResult exportWlInfoList(String orgid, String customerid, String startdate, String enddate
			, String status, String transtype) {
		List<Wlinfo> list = wlinfoService.exportWlInfoList(orgid, customerid, startdate, enddate, status
				,transtype);
		ExcelUtil<Wlinfo> util = new ExcelUtil<Wlinfo>(Wlinfo.class);
		return util.exportExcel(list, "wlinfo");
	}

	@Log(title = "获取拼车信息", businessType = BusinessType.OTHER)
	@GetMapping("/checkWlInfo")
	public AjaxResult checkWlInfo(String carCode, String orderid) {
		return wlinfoService.checkWlInfo(carCode, orderid);
	}


	@Log(title = "获取司机", businessType = BusinessType.OTHER)
	@GetMapping("/getWlDriver")
	public AjaxResult getWlDriver(String mobilephone) {
		return wlinfoService.getWlDriver(mobilephone);
	}

	@Log(title = "进入派车页面，获取数据", businessType = BusinessType.OTHER)
	@GetMapping("/toSendCar")
	public AjaxResult toSendCar(Long orderid, Integer orderType) {
		return wlinfoService.toSendCar(orderid, orderType);
	}

	/**
	 * @param
	 * @return void
	 * @throws
	 * @Title: getWlCompany
	 * @Description: 获取物流公司信息
	 * @author liukx
	 */
	@Log(title = "获取物流公司信息", businessType = BusinessType.OTHER)
	@GetMapping("/getWlCompany")
	public AjaxResult getWlCompany() {
		Supplier supplier = new Supplier();
		supplier.setStatus(1);
		supplier.setType(4);
		List<Supplier> customers = this.supplierMapper.selectSupplierList(supplier);
		return AjaxResult.success(customers);
	}

	/**
	 * 删除派车信息
	 */
	@RepeatSubmit
	@Log(title = "删除派车信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/deleteCar/{linkcode}")
	public AjaxResult deleteCar(@PathVariable String linkcode) {
		return wlinfoService.deleteCar(linkcode);
	}

}
